// load packages
var gulp     = require('gulp');
var $        = require('gulp-load-plugins')();
var config   = require("./gulp/config.js")();
var argv     = require('yargs').argv;
var rimraf   = require('rimraf');
var sequence = require('run-sequence');

$.sh       = require('shelljs');
// $.haml     = require('gulp-ruby-haml');
$.haml     = require('gulp-haml');
$.prettify = require('gulp-prettify');
$.browser  = require('browser-sync').create();

config.isProduction = !!(argv.production);

// load tasks
var gulpTaskList = require("fs").readdirSync("./gulp/tasks/");
gulpTaskList.forEach( function(taskfile) {
    require("./gulp/tasks/" + taskfile)(gulp, $, config);
});

gulp.task('check', function (done) {
  // 安装ruby haml
  if (!$.sh.which('ruby')) {
    console.log('  ' + $.util.colors.red('Ruby2.2.5 is not installed.'));
    $.sh.exit(1)
  }
  // 如果没有安装bundle就安装bundle
  if (!$.sh.which('bundle')) {
    console.log('  ' + $.util.colors.red('install bundler.'));
    $.sh.exec('gem install bundler');
  }
  // 没有安装haml就安装haml
  if (!$.sh.which('fastlane')) {
    console.log('  ' + $.util.colors.red('bundle install.'));
    $.sh.exec('bundle install');
  }

  if (!$.sh.test('-e', 'coffee/config.coffee')) {
    console.log('  ' + $.util.colors.red('copy config.coffee to config.'));
    $.sh.cp('coffee/config.coffee.sample', 'coffee/config.coffee')
  }

  if (!$.sh.test('-e', config.dest)) {
    console.log('  ' + $.util.colors.red('no dest dir: ' + config.dest));
    $.sh.mkdir(config.dest);
  }

  done();
});

// 默认
gulp.task('default', ['build']);

// task clean
gulp.task('clean', function(done) {
    rimraf('www/*', done);
});

// task all
gulp.task("all", function(done) {
    sequence(["check", "fonts", "img", "lib"], ["haml", "coffee", "sass"], done);
});

// task watch
gulp.task('watch', function() {
    gulp.watch(config.lib.path, ['lib']);
    gulp.watch(config.fonts.path, ['fonts']);
    gulp.watch(config.img.path, ['img']);
    gulp.watch(config.haml.path, ['haml']);
    gulp.watch(config.coffee.path, ['coffee']);
    gulp.watch(config.sass.path, ['sass']);
});

// 编译
gulp.task('build', function (done) {
  sequence('check', 'clean', ['fonts', 'img', "lib"], ['haml', 'coffee', 'sass'], done);
});

gulp.task('build-production', function (done) {
  config.isProduction = true;
  sequence('build', done);
});

// 拷贝配置文件
gulp.task('cp-config-dev', function(done) {
  $.sh.mv('coffee/config.coffee', 'coffee/configs/config.coffee.tmp');
  $.sh.cp('coffee/configs/config.coffee.dev', 'coffee/config.coffee');
  done();
});

gulp.task('cp-config-pro', function(done) {
  $.sh.mv('coffee/config.coffee', 'coffee/configs/config.coffee.tmp');
  $.sh.cp('coffee/configs/config.coffee.pro', 'coffee/config.coffee');
  done();
});

gulp.task('rm-config', function(done) {
  $.sh.rm('coffee/config.coffee');
  $.sh.mv('coffee/configs/config.coffee.tmp', 'coffee/config.coffee');
  done();
});

// 部署
gulp.task('deploy-dev', function (done) {
  var command = "rsync -avz -e 'ssh -p12227' ./www root@www.omniaccount.com:/home/deploy/work/omniknight3-dev/client-app";
  console.log(command);
  $.sh.exec(command);
});

gulp.task('deploy-pro', function (done) {
    var command = "rsync -avz -e 'ssh -p12229' ./www root@www.omniaccount.com:/home/deploy/www/oa-wechat";
    console.log(command);
    $.sh.exec(command);
});

// 发布
gulp.task('publish-dev', function (done) {
  sequence('cp-config-dev', 'build-production', 'rm-config', 'deploy-dev', done);
});

gulp.task('publish-pro', function (done) {
    sequence('cp-config-pro', 'build-production', 'rm-config', 'deploy-pro', done);
});
